package com.kinon.gfcanteen_v1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.bean.OrderDetailBean;
import com.kinon.gfcanteen_v1.bean.OrdersBean;
import com.kinon.gfcanteen_v1.view.activity.OrderDetailActivity;

import java.util.ArrayList;

/**
 * Created by lhqq on 2018-01-08.
 */

public class OrdersAdapter extends BaseAdapter {

    private ArrayList<OrdersBean.Orders> list;
    private Context mContext;
    private LayoutInflater inflater;

    public OrdersAdapter(ArrayList<OrdersBean.Orders> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        inflater=LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view==null){
            view= inflater.inflate(R.layout.item_order_layout,null);
            holder=new ViewHolder();
            holder.tv_orderStatus=view.findViewById(R.id.tv_orderStatus);
            holder.tv_orderTime=view.findViewById(R.id.tv_orderTime);
            holder.tv_orderUser=view.findViewById(R.id.tv_orderUser);
            holder.btn_confirm=view.findViewById(R.id.btn_confirm);
            holder.btn_seeDetail=view.findViewById(R.id.btn_seeDetail);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }

        holder.tv_orderTime.setText(list.get(i).getOrder_time());
        int state=list.get(i).getState();
        if(state==-1){
            holder.btn_confirm.setVisibility(View.GONE);
            holder.tv_orderStatus.setText("已取消");
        }else if(state==2){
            holder.btn_confirm.setVisibility(View.GONE);
            holder.tv_orderStatus.setText("已完成");
        }else if(state==0){
            holder.btn_confirm.setVisibility(View.GONE);
            holder.tv_orderStatus.setText("待支付");
        }else {
            holder.tv_orderStatus.setText("未处理");
            holder.btn_confirm.setVisibility(View.VISIBLE);
        }
        holder.tv_orderUser.setText(list.get(i).getName());
        holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intentDetail(i);
            }
        });
        holder.btn_seeDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intentDetail(i);
            }
        });
        return view;

    }

    private void intentDetail(int posotion){
        Intent intent=new Intent(mContext, OrderDetailActivity.class);
        intent.putExtra("orderid",list.get(posotion).getId()+"");
        mContext.startActivity(intent);
    }

    private class ViewHolder{
        TextView tv_orderStatus;
        TextView tv_orderTime;
        TextView tv_orderUser;
        Button btn_confirm;
        Button btn_seeDetail;
    }
}
